package com.syxbyi.hmwk1.main;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

/*
提示用户输入年月日信息，判断这一天是这一年中的第几天并打印
 */
public class PrintDayOfYear {
    private LocalDate localDateTime;
    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public void printDayOfYear() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("输入年月日信息，如2020 11 8：");
        // input check
        try {
            int year = scanner.nextInt();
            int month = scanner.nextInt();
            int day = scanner.nextInt();
            localDateTime = LocalDate.of(year, month, day);
        } catch (Exception e) {
            e.printStackTrace();
            localDateTime = LocalDate.now();
            System.out.println("输入错误，日期设置为默认值" + localDateTime.format(dateTimeFormatter));
        }
        System.out.println(this);
    }

    @Override
    public String toString() {
        return localDateTime.format(dateTimeFormatter) + "是这一年中的第" + localDateTime.getDayOfYear() + "天";
    }

    public static void main(String[] args) {
        PrintDayOfYear t = new PrintDayOfYear();
        t.printDayOfYear();
    }
}
